package com.swipemaster.dialogs;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;

import com.swipemaster.R;
import com.swipemaster.activities.GameActivity;
import com.swipemaster.common.Global;
import com.swipemaster.network.WorldRecord;
import com.swipemaster.record.Record;
import com.swipemaster.record.RecordId;

public class Dialogs
{
	/** Ensures that dialog size will be determined by content, not size of background image */
	public static void ensureSize(final View rootLayout)
	{
		final FrameLayout.LayoutParams rootParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);

		rootLayout.post(new Runnable()
		{
			@Override
			public void run()
			{
				rootLayout.setBackgroundColor(Color.TRANSPARENT);
				rootParams.height = rootLayout.getHeight();
				rootParams.width = rootLayout.getWidth();
				rootLayout.setBackgroundResource(R.drawable.dialog_h2_rounded);
				rootLayout.setLayoutParams(rootParams);
			}
		});
	}

	/** Shows dialog with your position in ranking after submitting a score. */
	public static void showAfterSubmit(Activity activity, int position)
	{
		final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setMessage(Global.app.getString(R.string.level_score_info1) + " " + position + " "
				+ Global.app.getString(R.string.level_score_info2));
		builder.setCancelable(false);
		builder.setPositiveButton("Ok", null);
		builder.create().show();
	}

	public static void showEntryTextDialog(final Activity activity, final RecordId recordId)
	{
		activity.runOnUiThread(new Runnable()
		{
			@Override
			public void run()
			{
				new EntryTextDialog(activity, recordId).show();
			}
		});
	}

	public static void showGameDialog(final GameActivity activity, final boolean isWinner, final boolean isNext,
			final Record record, final boolean isNewRecord)
	{
		activity.runOnUiThread(new Runnable()
		{
			@Override
			public void run()
			{
				if (!activity.isFinishing())
				{
					Global.app.dialog = new GameDialog(activity);
					Global.app.dialog.showOnGameOver(isWinner, isNext, record, isNewRecord);
				}
			}
		});
	}

	public static void showHelp(Activity activity, RecordId recordId)
	{
		new HelpDialog(activity, recordId).show();
	}

	public static void showInternetError(Activity activity)
	{
		final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setIcon(android.R.drawable.ic_delete);
		builder.setMessage(Global.app.getString(R.string.internet_error));
		builder.setCancelable(false);
		builder.setPositiveButton("Ok", null);
		builder.create().show();
	}

	public static void showRecords(Activity activity, RecordId recordType, ArrayList<WorldRecord> listOfRecords)
	{
		new RecordsDialog(activity, recordType, listOfRecords).show();
	}
}